define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/contract/index' + location.search,
                    add_url: 'user/contract/add',
                    edit_url: 'user/contract/edit',
                    del_url: 'user/contract/del',
                    multi_url: 'user/contract/multi',
                    import_url: 'user/contract/import',
                    table: 'user_contract',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'homeowner_id', title: __('Homeowner_id')},


                        {field: 'homeowner_name', title: __('Homeowner_name'), operate: 'LIKE'},
                        {field: 'homeowner_idcard', title: __('Homeowner_idcard'), operate: 'LIKE'},
                        {field: 'homeowner_phone', title: __('Homeowner_phone'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'house_address', title: __('House_address'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'money_zu', title: __('Money_zu'), operate:'BETWEEN'},
                        {field: 'money_ya', title: __('Money_ya'), operate:'BETWEEN'},
                        {field: 'pay_time', title: __('Pay_time'), operate: 'LIKE'},
                        {field: 'money_electricity', title: __('Money_electricity'), operate:'BETWEEN'},
                        {field: 'money_water', title: __('Money_water'), operate:'BETWEEN'},
                        {field: 'attachment', title: __('Attachment'), operate: 'LIKE'},
                        // {field: 'house_id', title: __('House_id')},
                        // {field: 'room_id', title: __('Room_id')},
                        // {field: 'build_id', title: __('Build_id')},
                        {field: 'build_address', title: __('Build_address'), operate: 'LIKE'},
                        {field: 'water_num', title: __('Water_num')},
                        {field: 'electricity_num', title: __('Electricity_num')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'ya_type', title: __('Ya_type'), searchList: {"1":__('Ya_type 1'),"2":__('Ya_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'room_name', title: __('Room_name'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'moon_chao_status', title: __('Moon_chao_status'), searchList: {"0":__('Moon_chao_status 0'),"1":__('Moon_chao_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'now_count', title: __('Now_count')},
                        {field: 'moon_fang_data', title: __('Moon_fang_data'), operate: 'LIKE'},
                        {field: 'user_name', title: __('User_name'), operate: 'LIKE'},
                        // {field: 'sex', title: __('Sex')},
                        {field: 'user_card', title: __('User_card'), operate: 'LIKE'},
                        {field: 'user_phone', title: __('User_phone'), operate: 'LIKE'},
                        // {field: 'user_card_img', title: __('User_card_img'), operate: 'LIKE'},
                        {field: 'user_type', title: __('User_type'), searchList: {"1":__('User_type 1'),"2":__('User_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'contact_name', title: __('Contact_name'), operate: 'LIKE'},
                        // {field: 'user_sign_img', title: __('User_sign_img'), operate: 'LIKE'},
                        // {field: 'rent_way', title: __('Rent_way')},
                        // {field: 'rent_day', title: __('Rent_day')},
                        {field: 'ahead_day', title: __('Ahead_day')},
                        {field: 'yongtu', title: __('Yongtu'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'first_zu', title: __('First_zu'), operate:'BETWEEN'},
                        // {field: 'del_uid', title: __('Del_uid')},
                        {field: 'roommate', title: __('Roommate'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'audit',
                                    text: __('审核'),
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'user/contract/audit',
                                    confirm: '确认要审核吗？',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        return false;
                                    },
                                    refresh: true ,
                                    visible:function(row){

                                        if (row.status==0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'revoke_audit',
                                    text: __('撤回'),
                                    title: __('撤回'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'user/contract/audit',
                                    confirm: '确认要撤回吗？',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        return false;
                                    },
                                    refresh: true ,
                                    visible:function(row){

                                        if (row.status!=0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: "bill_list",
                                    text:"账单",
                                    title: __("账单"),
                                    classname: "btn btn-xs btn-info btn-dialog bg-olive-active",
                                    icon: "fa",
                                    // extend: 'data-area=\'["90%", "80%"]\'',
                                    url: function (row) {
                                        return `bill?contract_id=${row.id}`;
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                }


                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/contract/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'user/contract/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'user/contract/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
